package com.happystudy.serviceImpl;

import com.happpystudy.model.User;
import com.happpystudy.model.course.Course;
import com.happpystudy.model.course.CourseKey;
import com.happpystudy.model.course.CourseTopic;
import com.happpystudy.service.CourseTopicService;
import com.happystudy.mapper.CourseTopicMapper;
import com.happystudy.util.KeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CourseTopicServiceImpl implements CourseTopicService {

    @Resource
    CourseTopicMapper courseTopicMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public List<CourseTopic> queryAllCourseTopicByCourseKey(String uniId, String courseId, Integer courseGroup) {

        CourseTopic courseKey = new CourseTopic();
        courseKey.setCourseId(courseId);
        courseKey.setUniId(uniId);
        courseKey.setCourseGroup(courseGroup);
        System.out.println(courseKey);
        return this.queryAllCourseTopicByCourseKey(courseKey);


    }

    @Override
    public List<CourseTopic> queryAllCourseTopicByCourseKey(CourseTopic courseKey) {
        return courseTopicMapper.selectByCourseKey(courseKey);
    }

    @Override
    public List<CourseTopic> queryCourseTopicByUniIdAndCourseId(String uniId,String courseId){
        CourseTopic courseTopic = new CourseTopic();
        courseTopic.setCourseId(courseId);
        courseTopic.setUniId(uniId);
        return courseTopicMapper.queryCourseTopicByUniIdAndCourseId(courseTopic);
    }

        @Override
    public boolean isCurrentTopicNameExist(String courseTopicName) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseTopic.class));
        String key = KeyUtil.COURSETOPIC_BY_NAME+":"+courseTopicName;
        CourseTopic courseTopic = (CourseTopic) redisTemplate.opsForValue().get(key);
        if (courseTopic==null){
            courseTopic = courseTopicMapper.selectByCourseTopicName(courseTopicName);
            if (courseTopic !=null){
                redisTemplate.opsForValue().set(key,courseTopic);
            }
        }
        return !(courseTopic==null);
    }

    @Override
    public CourseTopic doCreateCourseTopic(CourseTopic courseTopic) {
        int nextTopicId = courseTopicMapper.queryNextPrimaryKey();
        courseTopic.setTopicId(nextTopicId);
        int result = inserNewCourseTopic(courseTopic);
        if (result>0){
            return courseTopic;
        }
        return null;
    }

    @Override
    public CourseTopic queryCourseTopicByParimaryKey(Integer topicId) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseTopic.class));
        String key = KeyUtil.COURSETOPIC_BY_ID+":"+topicId;
        CourseTopic courseTopic = (CourseTopic) redisTemplate.opsForValue().get(key);
        if (courseTopic==null){
            courseTopic = courseTopicMapper.selectByPrimaryKey(topicId);
            if (courseTopic!=null){
                redisTemplate.opsForValue().set(key,courseTopic);
            }
        }
        return courseTopic;
    }

    @Override
    public int deleteByPK(Integer topicId) {

        return courseTopicMapper.deleteByPrimaryKey(topicId);
    }

    public int inserNewCourseTopic(CourseTopic courseTopic){

        int result = courseTopicMapper.insertSelective(courseTopic);
        if (result>0){
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseTopic.class));
            String key = KeyUtil.COURSETOPIC_BY_NAME+":" + courseTopic.getTopicName();
            redisTemplate.opsForValue().set(key,courseTopic);

            key = KeyUtil.joinKey(courseTopic);
            redisTemplate.opsForSet().add(key,courseTopic);
        }
        return result;
    }
}
